package com.zwk.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zwk.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zwk.gulimall.product.entity.AttrEntity;
import com.zwk.gulimall.product.entity.CategoryEntity;
import com.zwk.gulimall.product.service.AttrAttrgroupRelationService;
import com.zwk.gulimall.product.service.CategoryService;
import com.zwk.gulimall.product.vo.GroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zwk.gulimall.product.entity.AttrGroupEntity;
import com.zwk.gulimall.product.service.AttrGroupService;
import com.zwk.common.utils.PageUtils;
import com.zwk.common.utils.R;



/**
 * 属性分组
 *
 * @author zhuwenkang
 * @email zhuwenkang@gmail.com
 * @date 2022-12-30 22:47:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<CategoryEntity> catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        Long[] categoryLinkId=new Long[catelogPath.size()];
        for (int i = 0,len=catelogPath.size(); i < len; i++) {
            categoryLinkId[i]=catelogPath.get(i).getCatId();
        }
        return R.ok().put("attrGroup", attrGroup).put("categoryLinkId",categoryLinkId);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

//    /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> attrEntityList = attrGroupService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }

//    /product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable Long attrgroupId,@RequestParam Map<String,Object> params ){
        PageUtils noRelationAttr = attrGroupService.getNoRelationAttr(attrgroupId, params);
        return R.ok().put("page",noRelationAttr);
    }

//    /product/attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    public R removeRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntity){
        attrGroupService.removeRelation(relationEntity);
        return R.ok();
    }

//    /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody  List<AttrAttrgroupRelationEntity> relationEntity){
        relationService.saveBatch(relationEntity);
        return R.ok();
    }

//    /product/attrgroup/{catelogId}/withattr
    @GetMapping("{catelogId}/withattr")
    public R groupWithAttr(@PathVariable Long catelogId){
        List<GroupWithAttrVo> groupWithAttrVos=attrGroupService.groupWithAttrByCatId(catelogId);
        return R.ok().put("data",groupWithAttrVos);
    }
}
